package java_1_11.arrays_copy;

import java.util.Arrays;

public class AllCopy {
    public static void main(String[] args) {
        int[] arr={1,3,5,7,9};
        int[] ret= arrCopy(arr);//全拷贝
        int[] str=arrCopyRange(arr,2,4);//部份拷贝
        ret[0]=10;
        System.out.println(arrStr(arr));
        System.out.println(arrStr(ret));
        System.out.println(arrStr(str));
    }

    private static int[] arrCopy(int[] arr) {
        //实现数组全拷贝
        int[] ret=new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i]=arr[i];
        }
        return ret;
    }
    private static int[] arrCopyRange(int[] arr,int start,int end) {
        //部份拷贝
        int[] str=new int[end-start];
        int j=0;
        for (int i = 0; i < arr.length; i++) {
            while(i==start&&start<end){
                str[j]=arr[i];
                j++;
                start++;
            }
        }
        return str;
    }




    //    public static void main(String[] args) {
//        int[] arr={1,3,5,7,9};
//        int[] ret= Arrays.copyOf(arr,arr.length);
//        int[] ret= Arrays.copyOfRange(arr,2,4);//部份拷贝
//        System.out.println(arrStr(arr));
//        System.out.println(arrStr(ret));
//    }
//
    private static String arrStr(int[] arr) {
        String ret="[";
        for (int i = 0; i < arr.length; i++) {
            ret+=arr[i];
            if(i!= arr.length-1) {
                ret += ",";
            }
        }
        ret+="]";
        return ret;
    }

}
